﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace HBase {
    public class WCSClientHttpUtil {
        public static JArray getArray(String url) {
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(HttpRequestUrl.httpGet(url));
            if( jsonObject == null ) {
                return null;
            }
            object obj = jsonObject["data"];
            JArray data = obj as JArray;
            if( data == null ) {
                throw new Exception(obj.ToString());
            }
            return data;
        }
        public static JObject getObject(String url) {
            String value = HttpRequestUrl.httpGet(url);
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(value);

            if( jsonObject == null ) {
                return null;
            }
            var jsonData = jsonObject["data"];
            JObject data = ( JObject ) jsonData;
            return data;
        }
        public static T getEntity<T>(String url) {
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(HttpRequestUrl.httpGet(url));
            if( jsonObject == null || jsonObject["data"].Count() == 0 ) {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(jsonObject["data"].ToString());
        }


        public static List<T> getList<T>(String url) {
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(HttpRequestUrl.httpGet(url));
            if( jsonObject == null || jsonObject["data"].Count() == 0 ) {
                return default(List<T>);
            }
            return JsonConvert.DeserializeObject<List<T>>(jsonObject["data"].ToString());
        }
        public static JValue getJValue(String url) {
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(HttpRequestUrl.httpGet(url));
            if( jsonObject == null ) {
                return null;
            }
            JValue data = ( JValue ) jsonObject["data"];
            return data;
        }
    }
}
